//
//  THJMyOrderVC.m
//  THJXHW
//
//  Created by 河南德艺 on 17/3/30.
//  Copyright © 2017年 河南德艺. All rights reserved.
//

#import "THJMyOrderVC.h"
#import "THJMyOrderCell.h"
#import "ZBNetworking.h"
#import <MJRefresh.h>
#import "THJMyOrderDetailVC.h"
#import "RSA.h"
#import "THJTradeContractVC.h"
#import "THJTradeContractVC2.h"
#import "THJPartPayVC.h"
#import "THJAllPayVC.h"
#import "THJJieDongVC.h"
#import "THJContractShowVC.h"

@interface THJMyOrderVC ()<UITableViewDelegate,UITableViewDataSource,THJMyOrderCellDelegate>
{
    UITableView * _tableview;
    NSInteger pageNO;
    NSInteger btnIndex;
    NSString * statusName;
}
@property(nonatomic,strong)NSMutableArray * dataArr;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topSpace;

@end
@implementation THJMyOrderVC
-(NSMutableArray*)dataArr{
    if(!_dataArr){
        _dataArr = [[NSMutableArray alloc] init];
    }
    return _dataArr;
}
-(void)viewWillAppear:(BOOL)animated{
    self.tabBarController.tabBar.hidden = YES;
    self.navigationController.navigationBar.hidden = YES;
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    btnIndex = 2;
    pageNO = 1;
    [self setUpNav];
    [self showView];
    if(KIsiPhoneX){
        [self setXNavBarWithTitle:@"我的订单"];
    }else{
        [self setNavBarWithTitle:@"我的订单"];
    }
    [self getOrderListWithType:btnIndex];
    self.topSpace.constant = NavbarHeight;
   [self SVHUDShow];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshContent) name:@"paySuccess" object:nil];
    self.lineviewLeftSpace.constant = ScreenWidth/2;
}
-(void)refreshContent{
    NSLog(@"通知出发");
    [self getOrderListWithType:btnIndex];
}
-(void)getOrderListWithType:(NSInteger)type{
    [self SVHUDShow];
    //加密电话
    NSString * targetString = [THJTools getMobile];
    NSString *resultStr = [RSA encryptString:targetString publicKey:RSA_PubKey];
    //加密类型
    NSString * order_type = [RSA encryptString:[NSString stringWithFormat:@"%ld",type] publicKey:RSA_PubKey];
    NSString * url = @"http://p.thjb.net/thjblink/appdata.php?action=order_list";
    NSDictionary * dic = @{
                           @"type":@"ios",
                           @"mobile":resultStr,
                           @"order_type":order_type
                           };
    [THJNetWork postNetDataFromUrl:url parameters:dic callback:^(NSDictionary * receiveData, NSError *error) {
        [self SVHUDHiden];
        NSString * status = receiveData[@"status"];
        if([status isEqual:@"fail"]){
            NSLog(@"fail-77777---%@",receiveData);
        }else{
             NSLog(@"success-666---%@",receiveData);
            self.dataArr = receiveData[@"data"][@"list"];
            [_tableview reloadData];
        }
    }];
}
-(void)loadMore{
    pageNO++;
//    [self getDataWithPage:pageNO Obj:objStr];
    [_tableview.mj_footer endRefreshing];
}
-(void)showView{
    _tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, NavbarHeight+42, ScreenWidth, ScreenHeight - NavbarHeight - 42) style:UITableViewStylePlain];
    [_tableview registerNib:[UINib nibWithNibName:@"THJMyOrderCell" bundle:nil] forCellReuseIdentifier:@"order"];
    _tableview.tableFooterView = [[UIView alloc] init];
    _tableview.delegate = self;
    _tableview.dataSource = self;
//    _tableview.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMore)];
    _tableview.showsVerticalScrollIndicator = NO;
    _tableview.backgroundColor = COLOR(245, 245, 245, 1);
//    _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableview.rowHeight = 220;
    [self.view addSubview:_tableview];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArr.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    THJMyOrderCell * cell = [tableView dequeueReusableCellWithIdentifier:@"order" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.delegate = self;
    NSDictionary * dic = self.dataArr[indexPath.row];
    statusName =  [RSA decryptString:dic[@"status"] publicKey:RSA_PubKey];
    NSString * all_pay =  [RSA decryptString:dic[@"all_pay"] publicKey:RSA_PubKey];
     NSString * jd_pay =  [RSA decryptString:dic[@"jd_pay"] publicKey:RSA_PubKey];
     NSString * part_pay =  [RSA decryptString:dic[@"part_pay"] publicKey:RSA_PubKey];
    NSLog(@"all:%@-jd-%@-part-%@",all_pay,jd_pay,part_pay);
    NSLog(@"statusName=%@",statusName);
    if(btnIndex == 1){
        if([statusName isEqual:@"1"]){
            cell.btn1.hidden = cell.btn2.hidden = cell.btn3.hidden = YES;
        }else if([statusName isEqual:@"2"]){
            [cell.btn3 setTitle:@"卖家签合同" forState:UIControlStateNormal];
            cell.btn1.hidden = cell.btn2.hidden = YES;
            cell.btn3.hidden = NO;
        }else{
            [cell.btn3 setTitle:@"查看合同" forState:UIControlStateNormal];
            cell.btn1.hidden = cell.btn2.hidden = YES;
            cell.btn3.hidden = NO;
        }
    }else{
        if([statusName isEqual:@"1"]){
            cell.btn1.hidden = cell.btn2.hidden = YES;
            [cell.btn3 setTitle:@"买家签合同" forState:UIControlStateNormal];
        }else if([statusName isEqual:@"2"]){
            cell.btn1.hidden = cell.btn2.hidden = cell.btn3.hidden = YES;
        }else if([statusName isEqual:@"3"]){
            NSLog(@"status==3");
            cell.btn1.hidden = cell.btn2.hidden = cell.btn3.hidden = NO;
            [cell.btn3 setTitle:@"查看合同" forState:UIControlStateNormal];
            [cell.btn2 setTitle:@"部分支付" forState:UIControlStateNormal];
            [cell.btn1 setTitle:@"全额支付" forState:UIControlStateNormal];
            
        }else if([statusName isEqual:@"4"] || [statusName isEqual:@"5"]){
            cell.btn1.hidden = cell.btn2.hidden = cell.btn3.hidden = NO;
            [cell.btn3 setTitle:@"查看合同" forState:UIControlStateNormal];
            [cell.btn2 setTitle:@"部分支付" forState:UIControlStateNormal];
            [cell.btn1 setTitle:@"解冻资金" forState:UIControlStateNormal];
        }else{
            [cell.btn3 setTitle:@"查看合同" forState:UIControlStateNormal];
            cell.btn1.hidden = cell.btn2.hidden = YES;
            cell.btn3.hidden = NO;
        }
    }
    [cell setCellContentWithDic:dic btnIndex:btnIndex];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSDictionary * dic = self.dataArr[indexPath.row];
    THJMyOrderDetailVC * vc = [THJMyOrderDetailVC new];
    vc.itemid = [RSA decryptString:dic[@"itemid"] publicKey:RSA_PubKey];
    [self.navigationController pushViewController:vc animated:YES];
}
-(void)statusName1:(NSString *)statusName itemid:(NSString *)itemid all:(NSString *)all part:(NSString *)pat jd:(NSString *)jd{
    if(btnIndex == 2){
        if([statusName isEqual:@"3"]){
            NSLog(@"全额支付");
            if([all isEqual:@"1"]){
                THJAllPayVC * vc = [THJAllPayVC new];
                vc.itemid = itemid;
                [self.navigationController pushViewController:vc animated:YES];
            }else{
                [self HUDShowMsg:@"当前状态无法全额支付" AfterDelayDimiss:1];
            }
            
        }else if([statusName isEqual:@"4"]){
            NSLog(@"解冻资金-jd-%@",jd);
            if([jd isEqual:@"1"]){
                THJJieDongVC * vc = [THJJieDongVC new];
                vc.itemid = itemid;
                [self.navigationController pushViewController:vc animated:YES];
            }else{
                [self HUDShowMsg:@"当前状态无法解冻" AfterDelayDimiss:1];
            }
            
        }else if([statusName isEqual:@"5"]){
            NSLog(@"解冻资金-jd-%@",jd);
            if([jd isEqual:@"1"]){
                THJJieDongVC * vc = [THJJieDongVC new];
                vc.itemid = itemid;
                [self.navigationController pushViewController:vc animated:YES];
            }else{
                [self HUDShowMsg:@"当前状态无法解冻" AfterDelayDimiss:1];
            }
        }
    }
}
-(void)statusName2:(NSString *)statusName itemid:(NSString *)itemid all:(NSString *)all part:(NSString *)pat jd:(NSString *)jd{
    if(btnIndex == 2){
        if([statusName isEqual:@"3"] || [statusName isEqual:@"4"]||[statusName isEqual:@"5"]){
            NSLog(@"分批支付");
            if([pat isEqual:@"1"]){
                THJPartPayVC * vc = [THJPartPayVC new];
                vc.itemid = itemid;
                [self.navigationController pushViewController:vc animated:YES];
            }else{
                [self HUDShowMsg:@"当前状态无法分批支付" AfterDelayDimiss:1];
            }
        }
    }
}
-(void)statusName3:(NSString *)statusName itemid:(NSString *)itemid{
    if(btnIndex == 1){
        if([statusName isEqual:@"2"]){
            NSLog(@"卖家签合同");
            THJTradeContractVC * vc =  [THJTradeContractVC new];
            vc.orderId = itemid;
            NSLog(@"iteid-%@",itemid);
            [self.navigationController pushViewController:vc animated:YES];
        }else{
             NSLog(@"查看合同");
            THJContractShowVC * vc = [THJContractShowVC new];
            vc.orderId = itemid;
            [self.navigationController pushViewController:vc animated:YES];
        }
        
    }else{
        if([statusName isEqual:@"1"]){
            NSLog(@"买家签合同");
            THJTradeContractVC2 * vc =  [THJTradeContractVC2 new];
            vc.orderId = itemid;
            NSLog(@"iteid-%@",itemid);
            [self.navigationController pushViewController:vc animated:YES];
        }else{
             NSLog(@"查看合同");
            THJContractShowVC * vc = [THJContractShowVC new];
            vc.orderId = itemid;
            [self.navigationController pushViewController:vc animated:YES];
        }
    }
}
-(void)statusName4:(NSString *)statusName itemid:(NSString *)itemid{
    NSLog(@"订单详情");
    THJMyOrderDetailVC * vc = [THJMyOrderDetailVC new];
    vc.itemid = itemid;
    [self.navigationController pushViewController:vc animated:YES];
}
- (IBAction)backBtnAction:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
- (IBAction)btn1Action:(id)sender {
    NSLog(@"%ld",btnIndex);
    if(btnIndex == 1){
        NSLog(@"001");
        return;
    }else{
         NSLog(@"001--");
        btnIndex = 1;
        self.lineviewLeftSpace.constant = 0;
        [self getOrderListWithType:btnIndex];
    }
}
- (IBAction)btn2Action:(UIButton*)sender{
    NSLog(@"&&&-%ld",btnIndex);
    if(btnIndex == 2){
        NSLog(@"002");
        return;
    }else{
         NSLog(@"002--");
        btnIndex = 2;
//        [sender setTitleColor:SingleColor(blackColor) forState:UIControlStateNormal];
        self.lineviewLeftSpace.constant = ScreenWidth/2;
//        NSLog(@"%@",NSStringFromCGRect( self.lineView.frame));
//        self.lineView.frame = CGRectMake(ScreenWidth/2, 104, 375/2, 2);
//        objStr = @"seller";
        [self getOrderListWithType:btnIndex];
    }
    
}
-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"paySuccess" object:nil];
}
@end
